<template>
  <div class="home-container gradient-bg">
    <!-- 顶部状态栏 -->
    <div class="status-bar glass">
      <div class="status-content">
        <div class="connection-status">
          <span :class="['status-indicator', carStore.statusClass]"></span>
          <span class="status-text">{{ carStore.statusText }}</span>
        </div>
        <div class="battery-indicator">
          <el-icon><Cpu /></el-icon>
          <span>100%</span>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 标题区域 -->
      <div class="title-section">
        <h1 class="main-title">
          <el-icon class="title-icon"><Van /></el-icon>
          智能小车控制系统
        </h1>
        <p class="subtitle">专业的远程控制解决方案</p>
      </div>

      <!-- 功能卡片网格 -->
      <div class="feature-grid grid-responsive">
        <div class="feature-card card float" @click="navigateTo('/network-settings')">
          <div class="feature-icon">
            <el-icon size="48" color="var(--primary-color)">
              <Setting />
            </el-icon>
          </div>
          <h3 class="feature-title">网络设置</h3>
          <p class="feature-desc">配置小车连接参数</p>
          <div class="feature-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="feature-card card float" @click="navigateTo('/remote-control')">
          <div class="feature-icon">
            <el-icon size="48" color="var(--primary-color)">
              <VideoPlay />
            </el-icon>
          </div>
          <h3 class="feature-title">遥控控制</h3>
          <p class="feature-desc">按钮和摇杆控制</p>
          <div class="feature-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="feature-card card float" @click="navigateTo('/mecanum-wheel')">
          <div class="feature-icon">
            <el-icon size="48" color="var(--primary-color)">
              <Grid />
            </el-icon>
          </div>
          <h3 class="feature-title">麦克纳姆轮</h3>
          <p class="feature-desc">四轮独立控制</p>
          <div class="feature-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>
      </div>

      <!-- 快速操作区域 -->
      <div class="quick-actions card glass">
        <h3 class="section-title">
          <el-icon><Lightning /></el-icon>
          快速操作
        </h3>
        <div class="action-buttons button-group">
          <button 
            class="btn-secondary action-btn mobile-optimized" 
            @click="takePhoto"
            :disabled="!carStore.isConnected"
          >
            <el-icon><Camera /></el-icon>
            拍照
          </button>
          
          <button 
            class="btn-secondary action-btn mobile-optimized" 
            @click="toggleRecording"
            :disabled="!carStore.isConnected"
          >
            <el-icon><VideoCamera v-if="!carStore.isRecording" /><VideoPlay v-else /></el-icon>
            {{ carStore.isRecording ? '停止录像' : '开始录像' }}
          </button>
          
          <button 
            class="btn-secondary action-btn mobile-optimized" 
            @click="toggleTracking"
            :disabled="!carStore.isConnected"
          >
            <el-icon><Aim /></el-icon>
            {{ carStore.isTracking ? '停止循迹' : '开始循迹' }}
          </button>
        </div>
      </div>

      <!-- 状态信息卡片 -->
      <div class="status-card card glass">
        <h3 class="section-title">
          <el-icon><Monitor /></el-icon>
          系统状态
        </h3>
        <div class="status-grid">
          <div class="status-item">
            <span class="status-label">连接状态</span>
            <span :class="['status-value', carStore.statusClass]">
              {{ carStore.statusText }}
            </span>
          </div>
          <div class="status-item">
            <span class="status-label">录像状态</span>
            <span class="status-value">
              {{ carStore.isRecording ? '录制中' : '未录制' }}
            </span>
          </div>
          <div class="status-item">
            <span class="status-label">循迹状态</span>
            <span class="status-value">
              {{ carStore.isTracking ? '循迹中' : '未循迹' }}
            </span>
          </div>
          <div class="status-item">
            <span class="status-label">当前速度</span>
            <span class="status-value">
              X: {{ carStore.currentSpeed.x }}, Y: {{ carStore.currentSpeed.y }}
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部信息 -->
    <div class="footer glass">
      <p class="version">版本 1.0.0 | 智能小车控制系统</p>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCarStore } from '../stores/car'
import { commonApi } from '../utils/api'
import { 
  Setting, 
  VideoPlay, 
  Grid, 
  Camera, 
  VideoCamera, 
  Aim,
  ArrowRight,
  Lightning,
  Monitor,
  Van,
  Cpu
} from '@element-plus/icons-vue'

const router = useRouter()
const carStore = useCarStore()

// 页面加载时获取连接状态
onMounted(async () => {
  await carStore.getConnectionStatus()
})

// 导航到指定页面
const navigateTo = (path) => {
  router.push(path)
}

// 拍照
const takePhoto = async () => {
  const result = await carStore.cameraControl('photo')
  if (result.success) {
    commonApi.showSuccess('拍照成功')
  } else {
    commonApi.showError(result.message)
  }
}

// 切换录像状态
const toggleRecording = async () => {
  const action = carStore.isRecording ? 'record_stop' : 'record_start'
  const result = await carStore.cameraControl(action)
  if (result.success) {
    commonApi.showSuccess(carStore.isRecording ? '停止录像成功' : '开始录像成功')
  } else {
    commonApi.showError(result.message)
  }
}

// 切换循迹状态
const toggleTracking = async () => {
  const result = await carStore.trackingControl(!carStore.isTracking)
  if (result.success) {
    commonApi.showSuccess(carStore.isTracking ? '开始循迹成功' : '停止循迹成功')
  } else {
    commonApi.showError(result.message)
  }
}
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  position: relative;
  overflow-x: hidden;
}

/* 状态栏 */
.status-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 12px 20px;
  margin: 0;
}

.status-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.connection-status {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.status-text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.battery-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-primary);
  font-weight: 600;
}

/* 主要内容 */
.main-content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 标题区域 */
.title-section {
  text-align: center;
  margin-bottom: 20px;
}

.main-title {
  font-size: 32px;
  font-weight: 800;
  color: white;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.title-icon {
  font-size: 36px;
}

.subtitle {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
  font-weight: 500;
}

/* 功能卡片 */
.feature-card {
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.feature-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: var(--shadow-heavy);
}

.feature-icon {
  margin-bottom: 16px;
  display: flex;
  justify-content: center;
}

.feature-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 8px 0;
  text-align: center;
}

.feature-desc {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
  text-align: center;
  line-height: 1.5;
}

.feature-arrow {
  position: absolute;
  top: 20px;
  right: 20px;
  color: var(--primary-color);
  opacity: 0;
  transition: all 0.3s ease;
}

.feature-card:hover .feature-arrow {
  opacity: 1;
  transform: translateX(4px);
}

/* 快速操作 */
.quick-actions {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 20px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.action-buttons {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.action-btn {
  flex: 1;
  min-width: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px 20px;
  font-size: 14px;
  border-radius: 16px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: var(--gradient-secondary);
  border: 2px solid var(--primary-color);
  color: var(--primary-color);
  font-weight: 600;
}

.action-btn:hover:not(:disabled) {
  background: var(--primary-color);
  color: white;
  transform: translateY(-2px);
  box-shadow: var(--shadow-medium);
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* 状态卡片 */
.status-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.status-label {
  font-size: 14px;
  color: var(--text-secondary);
  font-weight: 500;
}

.status-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

/* 底部 */
.footer {
  padding: 16px 20px;
  text-align: center;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.version {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  margin: 0;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .main-content {
    padding: 16px;
    gap: 20px;
  }
  
  .main-title {
    font-size: 28px;
  }
  
  .title-icon {
    font-size: 32px;
  }
  
  .subtitle {
    font-size: 14px;
  }
  
  .feature-card {
    padding: 20px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .action-btn {
    width: 100%;
    min-width: auto;
  }
  
  .status-grid {
    grid-template-columns: 1fr;
  }
  
  .status-bar {
    padding: 10px 16px;
  }
}

/* 横屏适配 */
@media screen and (orientation: landscape) {
  .main-content {
    padding: 16px 24px;
  }
  
  .feature-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .action-buttons {
    flex-direction: row;
  }
  
  .status-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* 动画延迟 */
.feature-card:nth-child(1) { animation-delay: 0s; }
.feature-card:nth-child(2) { animation-delay: 0.2s; }
.feature-card:nth-child(3) { animation-delay: 0.4s; }
</style> 